/*
 * Copyright 2007 Johan Andries
 *
 * This file is released under the LGPL v2.1.
 * 
 * This file may be redistributed under the terms of the Library GNU Public
 * License.
 */

import java.io.IOException;

import org.hibernate.engine.SessionImplementor;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.runtime.CallbackFactory;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.BasicLibraryService;

import rubymatic.ActiveHibernateUtil;
import rubymatic.PersistentRubyArray;

public class ActiveHibernateInternalService implements BasicLibraryService {

	public boolean basicLoad(Ruby runtime) throws IOException {
		
		ActiveHibernateUtil.setRubyInstance(runtime);
		
		RubyModule mActiveHibernate = runtime.defineModule("ActiveHibernate");
		RubyClass cSequence = mActiveHibernate.defineClassUnder(
				"PersistentRubyArray", runtime.getObject(),new ObjectAllocator() {
	        public IRubyObject allocate(Ruby runtime, RubyClass klass) {
	            return new PersistentRubyArray(runtime, klass);
	        }
	    });
		
		CallbackFactory seq_cb = runtime.callbackFactory(PersistentRubyArray.class);
		
		cSequence.defineMethod("read", seq_cb.getMethod("ruby_read"));
		cSequence.defineMethod("write", seq_cb.getMethod("ruby_write"));
		
		return true;
	}

}
